#include <QTextCodec>
#include <QtCore/QCoreApplication>
#include "dryctrlthread.h"
#include "bcdidothread.h"
#include "KgGlobalData.h"
#include "io.h"
#include "bmsio.h"
#include <QDir>
#include "tcpmodbdev.h"
#include "unistd.h"

int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
	// 设置编码方式
	QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

	char *publishData = PUBLISH_DATE; // (ACYL)
	printf("XnExtIO Version %d.%d.%d RTDB %d.%d.%d Publish Date (Standard)%s\n",
	APP_VERSION_MAJOR,APP_VERSION_MIDDLE,APP_VERSION_MINOR,
	BAMS_VERSION_MAJOR,BAMS_VERSION_MINOR,BAMS_VERSION_REVISION,publishData);

	usleep(5 *1000 * 1000); 

	KgGlobalData::ms_isOutputLog = 1;
	int ret = KgGlobalData::InitRtdb(); //初始化Rtdb
	if (ret < 0)
	{
		printf("InitRtdb failed, ret:%d\n", ret);
		return -1;
	}
	KgGlobalData::readConfig();

	CIO io;
	CBMSIO newIo;
	
#ifdef BMS_IO_MODEL
/*
#ifdef BMS_XIN_PING
    CBMSIO io;
#else
	CIO io;
#endif
*/
    if (2 == KgGlobalData::ms_hardwareOemID || 3 == KgGlobalData::ms_hardwareOemID)// new hardware:Nan Bei Shan Ke Ji  // added 2024-5-1
    {
		if (newIo.init() < 0)
		{
			printf("Init newIO Server FAILED, Donot start service");
		}
		else
		{
			newIo.start();
		}
    }
	else // Fan Yi  Ke Ji
	{
		if (io.init() < 0)
		{
			printf("Init IO Server FAILED, Donot start service");
		}
		else
		{
			io.start();
		}
	}
#endif

	DryCtrlThread dryCtrlThread(NULL);
	KgGlobalData::ms_dryThd = &dryCtrlThread;
	BcDiDoThread bcThread(NULL);
	
	dryCtrlThread.start();
	bcThread.start();

	TcpModbDev dev;
	// 为2号BA，从1号BA读取消防信号等动环信号 (新疆盛全)
	// 为2号BA, 向1号BA写入从机数据 (楚能华锐)
	if (KgGlobalData::ms_devNo >= 2) // modified 2023-12-30
	{
		writeKgLogfile(QString("********Start TcpModbDev(%1, %2).").arg(g_brotherBaIp).arg(g_brotherBaPort));
		dev.setPara(g_brotherBaIp, g_brotherBaPort);
		//dev.start();
	}
	
	return a.exec();
}
